﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête dm_sent/sent</summary>
    /// <see cref="dm_sent"></see>
    //[DataContract]
    //public class dm_sent : direct_messages { }

    public delegate void dmSentEventHandler(object sender, dmSentEventArgs e);
    public class dmSentEventArgs : EventArgs
    {
        public dmSentEventArgs(ObservableCollection<direct_messages> dm)
        {
            _result = dm;
        }

        private ObservableCollection<direct_messages> _result;
        public ObservableCollection<direct_messages> result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event dmSentEventHandler dmSentCompleted;

        #region Direct Messages
        /// <summary>Permet de récupérer la liste des 20 derniers messages privés envoyés par l’utilisateur authentifié toutes familles confondues.</summary>
        /// <c>http://apitxt.hellotipi.com/direct_messages/sent.format</c>
        /// <param name="since">limite les résultats aux messages postés après cette date ; le même comprotement peut être obtenu en spécifiant l’entête HTTP If-Modified-Since dans la requête. Date au format RFC 2822</param>
        /// <param name="since_id">renvoie les messages postés après le message since_id</param>
        /// <param name="page">numéro de la page demandée</param>
        public void dmSent(DateTime? since = null, int? since_id = null, int? page = null)
        {
            base.APICallCompleted += new APICallCompletedEventHandler(dmSentCallCompleted);

            var querystring = new List<KeyValuePair<string, string>> { };

            if (since != null)
                querystring.Add(new KeyValuePair<string, string>("since", since.ToString()));

            else if (since_id != null)
                querystring.Add(new KeyValuePair<string, string>("since_id", since_id.ToString()));

            else if (page != null)
                querystring.Add(new KeyValuePair<string, string>("page", since.ToString()));

            GetUrlApi("direct_messages/sent.json", querystring);
        }

        void dmSentCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                dmSentCompleted.Invoke(sender, new dmSentEventArgs(serializeTools.Deserialize<ObservableCollection<direct_messages>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(dmSentCallCompleted);
            }
        }

        #endregion
    }

}

